package com.mall.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.mall.admin.exception.BusinessException;
import com.mall.commons.utils.PageResult;
import com.mall.domain.PmsBrand;
import com.mall.interface1.api.PmsBrandApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsBrandService {

    @DubboReference
    private PmsBrandApi pmsBrandApi;

    /**
     * 获取全部品牌列表
     * @return
     */
    public List<PmsBrand> listAll() {
        return pmsBrandApi.findAllBrand();
    }

    /**
     * 根据品牌名称分页获取品牌列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult list(Integer pageNum, Integer pageSize, String keyword) {
        //根据条件分页查询出品牌名称列表
        List<PmsBrand>psList=pmsBrandApi.list(pageNum,pageSize,keyword);
        //查询符合条件的总记录条数
        Integer count=pmsBrandApi.count(pageNum,pageSize,keyword);
        PageResult pageResult=new PageResult(pageNum,pageSize,count,psList);
        return pageResult;
    }

    /**
     * 添加品牌
     * @param pmsBrand
     * @return
     */
    public Integer create(PmsBrand pmsBrand) {
        return pmsBrandApi.save(pmsBrand);
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    public Integer delete(Long id) {
        List<Long>list=new ArrayList<>();
        list.add(id);
        return pmsBrandApi.deleteByIds(list);
    }

    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    public Integer deleteBatch(List<Long> ids) {
        return pmsBrandApi.deleteByIds(ids);
    }

    /**
     * 批量更新厂家制造商状态
     * @param ids
     * @param factoryStatus
     */
    public void factoryStatus(List<Long> ids, Integer factoryStatus) {
        if (CollUtil.isNotEmpty(ids)){
            for (Long id : ids) {
                PmsBrand pmsBrand=new PmsBrand();
                pmsBrand.setId(id);
                pmsBrand.setFactoryStatus(factoryStatus);
                Integer count=pmsBrandApi.updateBrand(pmsBrand);
                if (count==null){
                    throw new BusinessException("无信息");
                }
            }
        }
    }

    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     */
    public void showStatus(List<Long> ids, Integer showStatus) {
        if (CollUtil.isNotEmpty(ids)){
            for (Long id : ids) {
                PmsBrand pmsBrand=new PmsBrand();
                pmsBrand.setId(id);
                pmsBrand.setFactoryStatus(showStatus);
                Integer count=pmsBrandApi.updateBrand(pmsBrand);
                if (count==null){
                    throw new BusinessException("无信息");
                }
            }
        }
    }

    /**
     * 更新品牌
     * @param id
     * @param pmsBrand
     * @return
     */
    public Integer updateBrand(Long id, PmsBrand pmsBrand) {
        pmsBrand.setId(id);
        return pmsBrandApi.updateBrand(pmsBrand);
    }

    /**
     * 根据编号查询品牌信息
     * @param id
     * @return
     */
    public PmsBrand query(Long id) {
        return pmsBrandApi.findById(id);
    }
}
